<?php
  session_start();
  header("content-type=textml; charset=utf-8");  //设置字符集
  require("config.php");  //包含数据库连接文件
  $username  = $_POST['username']; //获取用户姓名
  $password  = $_POST['password'];  //获取密码
  $depart  = $_POST['depart'];  

  //判断用户名是否存在
  $sql="SELECT u_name FROM users";
  $re = mysql_query($sql);
  $user = mysql_fetch_array($re);
  if(function_exists($user['u_name']))
  {
	 echo "<script>alert('用户名已存在，请重新输入！');window.location.href='sign_up.php';</script>";
  }
  
 if($username== "" || $password== "")
  {
	 echo "<script>alert('用户名密码不允许为空！');window.location.href='sign_up.php';</script>";
	 exit;
  }
  $insert_sql="INSERT INTO users (u_name,u_pwd,is_admin,d_id) VALUES ('$username', '$password','0','$depart')";
  $rs = mysql_query($insert_sql);// 返回结果集，成功true，失败false
  if($rs && mysql_affected_rows()>0)
  {
	  echo "<script>alert('用户注册成功！');window.location.href='signin.html';</script>";
  }
//mysql_free_result($result);
//mysql_free_result($rs);
mysql_close($con);
 ?>